package icu.groot.alan.servlet.task;

import icu.groot.alan.entity.Announce;
import icu.groot.alan.entity.Task;
import icu.groot.alan.entity.TokenEntity;
import icu.groot.alan.service.AnnounceService;
import icu.groot.alan.service.TaskService;
import icu.groot.alan.service.impl.AnnounceServiceImpl;
import icu.groot.alan.service.impl.TaskServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/task/list.do")
public class TaskListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询所有存在的任务
        TaskService taskService = new TaskServiceImpl();
        List<Task> tasks = taskService.getAllTask();
        //管理员信息
        TokenEntity tokenEntity = (TokenEntity) req.getAttribute("tokenEntity");
        //查询所有发布的任务
        AnnounceService announceService = new AnnounceServiceImpl();
        List<Announce> allAnnounces = announceService.getAllAnnounceTask();
        if (tokenEntity.getDepartment().equals("管理员")){
            req.setAttribute("allAnnounces",allAnnounces);
        }
        //查询需要完成的任务
        List<Announce> needComplete = announceService.getNeedComplete(tokenEntity.getCode());
        req.setAttribute("needComplete", needComplete);
        req.setAttribute("identity", tokenEntity.getDepartment());
        req.setAttribute("tasks",tasks);
        req.setAttribute("navActive","navTask");
        req.getRequestDispatcher("/task.jsp").forward(req, resp);
    }
}
